package com.uduew.sjd.ui.mine

import android.os.Bundle
import android.view.View
import com.uduew.sjd.BR
import com.uduew.sjd.R
import com.uduew.sjd.api.GetBankInfoApi
import com.uduew.sjd.message.Messages
import com.uduew.sjd.message.PageMessenger
import com.uduew.sjd.net.DataResult
import com.uduew.sjd.request.GetBankInfoRequester
import com.uduew.sjd.ui.adapter.BankListAdapter
import com.uduew.sjd.ui.base.BaseFragment
import com.uduew.sjd.ui.base.StateHolder
import com.uduew.sjd.utils.StatusBarUtil
import com.uduew.sjd.utils.ToastUtils
import com.uduew.sjd.utils.Utils
import com.hjq.bar.OnTitleBarListener
import com.hjq.bar.TitleBar
import com.kongzue.dialogx.dialogs.WaitDialog
import com.kunminx.architecture.ui.page.DataBindingConfig
import com.kunminx.architecture.ui.state.State
import com.uduew.sjd.request.SetBankInfoRequester

class BankListFragment : BaseFragment() {
    private lateinit var mAdapter: BankListAdapter

    lateinit var mState: BankListStates
    private lateinit var mGetBankInfoRequester: GetBankInfoRequester
    private lateinit var mMessenger: PageMessenger
    private lateinit var mSetBankInfoRequester: SetBankInfoRequester

    private var selectItem: GetBankInfoApi.HesitancyBean? = null

    override fun initViewModel() {
        mState = getFragmentScopeViewModel(BankListStates::class.java)
        mGetBankInfoRequester = getFragmentScopeViewModel(GetBankInfoRequester::class.java)
        mMessenger = getApplicationScopeViewModel(PageMessenger::class.java)

        mSetBankInfoRequester = getFragmentScopeViewModel(SetBankInfoRequester::class.java)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        StatusBarUtil.setStatusBar(requireActivity(), false, 0, true)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        var darkContent = arguments?.getBoolean("darkContent")
        darkContent.let {
            if (it != null) {
                StatusBarUtil.setStatusBar(requireActivity(), it, 0, true)
            }
        }
        mMessenger.input(Messages(Messages.EVENT_SELECT_BACK))
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mMessenger.output(this) { messages ->
            if (messages.eventId == Messages.EVENT_ADD_BANK_SUCCESS) {
                WaitDialog.show(getString(R.string.loading))
                mGetBankInfoRequester.request(this)
            }
        }
        mGetBankInfoRequester.result.observe(viewLifecycleOwner) { dataResult: DataResult<GetBankInfoApi.ResGetBankInfoBean> ->
            WaitDialog.dismiss()
            if (!dataResult.responseStatus.isSuccess) {
                ToastUtils.showShortToast(dataResult.responseStatus.responseCode)
                return@observe
            }
            dataResult.result.let {
                if (it != null) {
                    mState.backList.set(it.hesitancy)
                }
            }
        }
        WaitDialog.show(getString(R.string.loading))
        mGetBankInfoRequester.request(this)
    }

    override fun getDataBindingConfig(): DataBindingConfig {
        mAdapter = BankListAdapter(requireContext())

        mAdapter.setOnItemClickListener { id, item, position ->
            if (!Utils.checkDebounce()) return@setOnItemClickListener
            mAdapter.currentList.forEach {
                if (it.isSelected) it.isSelected = false
            }
            item.isSelected = !item.isSelected;
            selectItem = item
            mAdapter.notifyDataSetChanged()
        }

        return DataBindingConfig(R.layout.fragment_bank_list, BR.vm, mState)
            .addBindingParam(BR.adapter, mAdapter)
            .addBindingParam(BR.click, ClickProxy())
            .addBindingParam(BR.titleBarListener, TitleBarListener())
    }

    inner class TitleBarListener : OnTitleBarListener {
        override fun onLeftClick(titleBar: TitleBar?) {
            super.onLeftClick(titleBar)
            if (!Utils.checkDebounce()) return
            nav().navigateUp()
        }

        override fun onRightClick(titleBar: TitleBar?) {
            super.onRightClick(titleBar)
        }
    }


    class BankListStates : StateHolder() {
        val backList: State<ArrayList<GetBankInfoApi.HesitancyBean>> =
            State<ArrayList<GetBankInfoApi.HesitancyBean>>(ArrayList<GetBankInfoApi.HesitancyBean>())

    }

    open inner class ClickProxy {
        fun addBank() {
            if (!Utils.checkDebounce()) return
            nav().navigate(R.id.action_navigation_list_to_add_fragment)
        }

        fun selectBank() {
            if (!Utils.checkDebounce()) return
            selectItem.let {
                var isLoan = arguments?.getBoolean("isLoan", false)
                if (isLoan == true) {
                    mMessenger.input(
                        Messages(
                            Messages.EVENT_SET_BANK_DEF,
                            it?.yod,
                            it?.melaleuca
                        )
                    )
                    mSetBankInfoRequester.request(this@BankListFragment, it?.beastly)
                } else {
                    mSetBankInfoRequester.request(this@BankListFragment, it?.beastly)
                }
            }
            nav().navigateUp()
        }
    }

}